import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { useIcon } from '@/hooks/web/useIcon'
const testEnd = useIcon({ icon: 'svg-icon:testEnd' })
const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/home/index',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/home',
    component: Layout,
    redirect: '/home/index',
    name: 'Home',
    meta: {
      title: "数据统计"
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/Home/index.vue'),
        meta: {
          title: "数据统计",
          icon: 'svg-icon:shouye'
        }
      }
    ]


  },
  {
    path: '/business',
    component: Layout,
    redirect: '/business/index',
    name: 'Business',
    meta: {
      title: "企业管理",
    },
    children: [
      {
        path: 'index',
        name: 'BusinessIndex',
        component: () => import('@/views/Business/index.vue'),
        meta: {
          title: "企业管理",
          icon: 'svg-icon:testbefores'
        }
      }
    ]
  },
  {
    path: '/job',
    component: Layout,
    redirect: '/job/index',
    name: 'Job',
    meta: {
      title: "职位管理",
    },
    children: [
      {
        path: 'index',
        name: 'JobIndex',
        component: () => import('@/views/Job/index.vue'),
        meta: {
          title: "职位管理",
          icon: 'svg-icon:teststart'
        }
      }
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    name: 'User',
    meta: {
      title: "用户管理",
    },
    children: [
      {
        path: 'index',
        name: 'UserIndex',
        component: () => import('@/views/User/index.vue'),
        meta: {
          title: "用户管理",
          icon: 'svg-icon:admin'
        }
      }
    ]
  },
  {
    path: '/column',
    component: Layout,
    redirect: '/column/index',
    name: 'Column',
    meta: {
      title: "官网栏目管理",
    },
    children: [
      {
        path: 'index',
        name: 'ColumnIndex',
        component: () => import('@/views/Column/index.vue'),
        meta: {
          title: "官网栏目管理",
          icon: 'svg-icon:testing'
        }
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    redirect: '/setting/index',
    name: 'Setting',
    meta: {
      title: "官网配置",
      icon: 'svg-icon:testing',
      alwaysShow: true
    },
    children: [
      {
        path: 'indexs',
        name: 'Indexs',
        component: Layout,
        meta: {
          title: "首页配置",
          alwaysShow: true
          // icon: 'svg-icon:admin'
        },
        children: [
          {
            path: 'index',
            name: 'index-index',
            component: () => import('@/views/Setting/Index/index.vue'),
            meta: {
              title: "首页图文",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'index_ad',
            name: 'index-ad',
            component: () => import('@/views/Setting/Index/index_ad.vue'),
            meta: {
              title: "广告位",
              // icon: 'svg-icon:admin'
            },
          }
        ],

      },
      {
        path: 'Build-index',
        name: 'build-index',
        component: Layout,
        meta: {
          title: "党建引领",
          alwaysShow: true
          // icon: 'svg-icon:testbefores'
        },
        children: [
          {
            path: 'Build_image',
            name: 'Build_image',
            component: () => import('@/views/Setting/Build/index.vue'),
            meta: {
              title: "党建图文",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Build_info',
            name: 'Build_info',
            component: () => import('@/views/Setting/Build/Build_info.vue'),
            meta: {
              title: "党建信息",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Build_activity',
            name: 'Build_activity',
            component: () => import('@/views/Setting/Build/Build_activity.vue'),
            meta: {
              title: "党建活动回顾",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Build_way',
            name: 'Build_way',
            component: () => import('@/views/Setting/Build/Build_way.vue'),
            meta: {
              title: "党员风采",
              // icon: 'svg-icon:admin'
            },
          }
        ],

      },
      {
        path: 'Human-index',
        name: 'human-index',
        component: Layout,
        meta: {
          title: "人资招聘",
          alwaysShow: true
          // icon: 'svg-icon:testbefores'
        },
        children: [
          {
            path: 'Human-xintou',
            name: 'human-xintou',
            component: () => import('@/views/Setting/Human/index.vue'),
            meta: {
              title: "新投集团招聘",
              // icon: 'svg-icon:admin'
            },
          },
        ]

      },
      {
        path: 'Notices-index',
        name: 'notices-index',
        component: Layout,
        meta: {
          title: "公示公告",
          alwaysShow: true
          // icon: 'svg-icon:testbefores'
        },
        children: [
          {
            path: 'Notices-indexs',
            name: 'Notices-indexs',
            component: () => import('@/views/Setting/Notice/index.vue'),
            meta: {
              title: "公示公告",
              // icon: 'svg-icon:admin'
            },
          },
        ]
      },
      {
        path: 'Xindu-index',
        name: 'xindu-index',
        component: Layout,
        meta: {
          title: "新都特色",
          alwaysShow: true
          // icon: 'svg-icon:testbefores'
        },
        children: [
          {
            path: 'Xindu_detail',
            name: 'Xindu_detail',
            component: () => import('@/views/Setting/Xindu/index.vue'),
            meta: {
              title: "新都简介",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Xindu_honor',
            name: 'Xindu_honor',
            component: () => import('@/views/Setting/Xindu/Xindu_honor.vue'),
            meta: {
              title: "新都荣誉",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Xindu_memoirs',
            name: 'Xindu_memoirs',
            component: () => import('@/views/Setting/Xindu/Xindu_memoirs.vue'),
            meta: {
              title: "新都大事记",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Xindu_milestone',
            name: 'Xindu_milestone',
            component: () => import('@/views/Setting/Xindu/Xindu_milestone.vue'),
            meta: {
              title: "新都里程碑",
              // icon: 'svg-icon:admin'
            },
          },
          {
            path: 'Xindu_characteristic',
            name: 'Xindu_characteristic',
            component: () => import('@/views/Setting/Xindu/Xindu_characteristic.vue'),
            meta: {
              title: "新都特色",
              // icon: 'svg-icon:admin'
            },
          }
        ],

      },
      {
        path: 'Guide-index',
        name: 'guide-index',
        component: Layout,
        meta: {
          title: "办事指南",
          alwaysShow: true
          // icon: 'svg-icon:testbefores'
        },
        children: [
          {
            path: 'Guide-indexs',
            name: 'Guide-indexs',
            component: () => import('@/views/Setting/Guide/index.vue'),
            meta: {
              title: "办事指南",
              // icon: 'svg-icon:admin'
            },
          },
        ]

      }, {
        path: 'Train-index',
        name: 'train-index',
        component: Layout,
        meta: {
          title: "人才培训",
          alwaysShow: true
          // icon: 'svg-icon:testbefores'
        },
        children: [
          {
            path: 'Train-indexs',
            name: 'Train-indexs',
            component: () => import('@/views/Setting/Train/index.vue'),
            meta: {
              title: "人才培训",
              // icon: 'svg-icon:admin'
            },
          },
        ]

      },
      // {
      //   path: 'Module-index',
      //   name: 'module-index',
      //   component: () => import('@/views/Setting/Module/index.vue'),
      //   meta: {
      //     title: "模块参数",
      //     // icon: 'svg-icon:testbefores'
      //   }, 
      // }
    ]
  },
  {
    path: '/roles',
    component: Layout,
    redirect: '/roles/index',
    name: 'Roles',
    meta: {
      title: "角色管理",
    },
    children: [
      {
        path: 'index',
        name: 'roles-index',
        component: () => import('@/views/Roles/index.vue'),
        meta: {
          title: "角色管理",
          icon: 'svg-icon:admin'
        }
      }
    ]
  },
  {
    path: '/menu',
    component: Layout,
    redirect: '/menu/index',
    name: 'Menu',
    meta: {
      title: "菜单管理",
    },
    children: [
      {
        path: 'index',
        name: 'menu-index',
        component: () => import('@/views/Menu/index.vue'),
        meta: {
          title: "菜单管理",
          icon: 'svg-icon:jiegou'
        }
      }
    ]
  },
  {
    path: '/dictionary',
    component: Layout,
    redirect: '/dictionary/index',
    name: 'Dictionary',
    meta: {
      title: "字典管理",
    },
    children: [
      {
        path: 'index',
        name: 'dictionary-index',
        component: () => import('@/views/Dictionary/index.vue'),
        meta: {
          title: "字典管理",
          icon: 'svg-icon:testbefores'
        }
      },
      {
        path: 'Dictionary_Detail-index/:id',
        name: 'dictionary_Detail-index',
        component: () => import('@/views/Dictionary/detail.vue'),
        meta: {
          hidden: true,
          title: "字典详情",
          canTo: true,
          activeMenu: '/dictionary'
        }
      }
    ]
  },
  {
    path: '/notice',
    component: Layout,
    redirect: '/notice/index',
    name: 'Notice',
    meta: {
      title: "通知公告",
    },
    children: [
      {
        path: 'index',
        name: 'notice-index',
        component: () => import('@/views/Notice/index.vue'),
        meta: {
          title: "通知公告",
          icon: 'svg-icon:testEnd'
        }
      }
    ]
  },
  {
    path: '/administrator',
    component: Layout,
    redirect: '/administrator/index',
    name: 'Administrator',
    meta: {
      title: "账号管理",
    },
    children: [
      {
        path: 'index',
        name: 'administrator-index',
        component: () => import('@/views/Administrator/index.vue'),
        meta: {
          title: "账号管理",
          icon: 'svg-icon:admin'
        }
      }
    ]
  },
  {
    path: '/agreement',
    component: Layout,
    redirect: '/agreement/index',
    name: 'Agreement',
    meta: {
      title: "协议配置",
    },
    children: [
      {
        path: 'index',
        name: 'agreement-index',
        component: () => import('@/views/Agreement/index.vue'),
        meta: {
          title: "协议配置",
          icon: 'svg-icon:admin'
        }
      }
    ]
  },
  {
    path: '/feedback',
    component: Layout,
    redirect: '/feedback/index',
    name: 'Feedback',
    meta: {
      title: "反馈意见",
    },
    children: [
      {
        path: 'index',
        name: 'feedback-index',
        component: () => import('@/views/Feedback/index.vue'),
        meta: {
          title: "反馈意见",
          icon: 'svg-icon:testEnd'
        }
      }
    ]
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
